package cn.com.headfree.sysmenu.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.sysmenu.entity.TSysMenu;
import cn.com.headfree.sysmenu.service.IMenuService;
import cn.com.headfree.sysmenu.vo.TreeSelect;
import cn.com.headfree.tjfx.controller.AnalysisController;
import com.gta.edu.sdk.common.dto.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//菜单信息
@Api(value = "菜单信息",tags = {"菜单信息接口"})//描述controller
@RestController
@RequestMapping(value = "/sysMenu", produces = "application/json;charset=UTF-8")
public class SysMenuController extends ExBaseController {
    private static final Logger LOG = LogManager.getLogger(SysMenuController.class);

    @Autowired
    IMenuService MenuService;

    @ApiOperation(value = "获取菜单全部列表",notes = "获取菜单全部列表",httpMethod = "POST")//描述接口
    @RequestMapping("/getMenuList")
    public ResultData getMenuList(@RequestBody TSysMenu sysMenu,HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
//            User user = this.getLoginUserInfo(request);
            List<TSysMenu> list = MenuService.findMenuList(sysMenu,new User());
            rs.setData(list);
            LOG.info("查询菜单信息成功！");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("查询菜单信息失败！");
            e.printStackTrace();
        }
        return rs;
    }

    @ApiOperation(value = "根据菜单id获取详细信息",notes = "根据菜单id获取详细信息",httpMethod = "GET")//描述接口
    @RequestMapping("/getMenuById")
    public ResultData getMenuById(String menuId, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
            rs.setSuccess(false);
            if(StringUtils.isBlank(menuId)){
                rs.setErrorMsg("菜单id不能为空！");
                return rs;
            }
            TSysMenu sysMenu = new TSysMenu();
            sysMenu.setId(menuId);
//            User user = this.getLoginUserInfo(request);
            List<TSysMenu> list = MenuService.findMenuList(sysMenu,new User());
            if(list.size()==0){
                rs.setErrorMsg("菜单不存在！");
                return rs;
            }
            rs.setData(list.get(0));
            rs.setSuccess(true);
            LOG.info("根据菜单id获取详细信息成功！");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("根据菜单id获取详细信息失败！");
            e.printStackTrace();
        }
        return rs;
    }

    @ApiOperation(value = "获取菜单下拉树列表",notes = "获取菜单下拉树列表",httpMethod = "POST")//描述接口
    @RequestMapping("/getMenuTree")
    public ResultData getMenuTree(@RequestBody TSysMenu sysMenu, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
//            User user = this.getLoginUserInfo(request);
            List<TSysMenu> list = MenuService.findMenuList(sysMenu,new User());
            List<TreeSelect> treeSelects = MenuService.buildMenuTreeSelect(list);
            rs.setData(treeSelects);
            rs.setSuccess(true);
            LOG.info("获取菜单下拉树列表成功！");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("获取菜单下拉树列表失败！");
            e.printStackTrace();
        }
        return rs;
    }

    @ApiOperation(value = "加载对应角色菜单列表树",notes = "加载对应角色菜单列表树",httpMethod = "POST")//描述接口
    @RequestMapping("/getRoleMenuTree")
    public ResultData getRoleMenuTree(@RequestParam(value = "roleId") String roleId, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
//            User user = this.getLoginUserInfo(request);
            List<TSysMenu> list = MenuService.findMenuList(new TSysMenu(),new User());
            List<TreeSelect> menus = MenuService.buildMenuTreeSelect(list);
            Map map = new HashMap<>();
            map.put("menus",menus); //全部菜单
            if(StringUtils.isBlank(roleId)){
                map.put("checkedKeys",""); //角色对应菜单id
            }else{
                List<String> checkedKeys = MenuService.selectMenuIdListByRoleId(roleId);
                map.put("checkedKeys",checkedKeys); //角色对应菜单id
            }

            rs.setData(map);
            rs.setSuccess(true);
            LOG.info("获取菜单下拉树列表成功！");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("获取菜单下拉树列表失败！");
            e.printStackTrace();
        }
        return rs;
    }

    @ApiOperation(value = "新增菜单",notes = "新增菜单",httpMethod = "POST")//描述接口
    @RequestMapping("/addMenu")
    public ResultData addMenu(@RequestBody TSysMenu sysMenu, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
            if(StringUtils.isBlank(sysMenu.getMenuName())){
                rs.setErrorMsg("菜单名称不能为空！");
                return rs;
            }
            List<TSysMenu> list = MenuService.findMenuListByMenuName(null,sysMenu.getMenuName());
            if(list.size()>0){
                rs.setErrorMsg("菜单名称已经存在！");
                return rs;
            }
            MenuService.addMenu(sysMenu);
            rs.setSuccess(true);
            LOG.info("新增菜单成功！");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("新增菜单失败！");
            e.printStackTrace();
        }
        return rs;
    }

    @ApiOperation(value = "更新菜单",notes = "更新菜单",httpMethod = "POST")//描述接口
    @RequestMapping("/updateMenu")
    public ResultData updateMenu(@RequestBody TSysMenu sysMenu, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
            rs.setSuccess(false);
            if(StringUtils.isBlank(sysMenu.getId())){
                rs.setErrorMsg("菜单id不能为空！");
                return rs;
            }
            if(StringUtils.isBlank(sysMenu.getMenuName())){
                rs.setErrorMsg("菜单名称不能为空！");
                return rs;
            }
            List<TSysMenu> list = MenuService.findMenuListByMenuName(sysMenu.getId(),sysMenu.getMenuName());
            if(list.size()==0){ //菜单名称已改
                List<TSysMenu> list1 = MenuService.findMenuListByMenuName(null,sysMenu.getMenuName());
                if(list1.size()>0){
                    rs.setErrorMsg("菜单名称已经存在！");
                    return rs;
                }
            }
            MenuService.updateMenu(sysMenu);
            rs.setSuccess(true);
            LOG.info("更新菜单成功！");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("更新菜单失败！");
            e.printStackTrace();
        }
        return rs;
    }

    @ApiOperation(value = "验证菜单名称是否唯一",notes = "验证菜单名称是否唯一",httpMethod = "POST")//描述接口
    @RequestMapping("/checkMenuName")
    public ResultData checkMenuName(@RequestBody TSysMenu sysMenu, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
            rs.setSuccess(true);
            HashMap map = new HashMap();
            Boolean flag = false;
            if (StringUtils.isBlank(sysMenu.getMenuName())) {
                map.put("flag",flag);
                rs.setData(map);
                rs.setErrorMsg("菜单名称不能为空！");
                return rs;
            }
            if(StringUtils.isBlank(sysMenu.getId())){  // 新增
                List<TSysMenu> list = MenuService.findMenuListByMenuName(null,sysMenu.getMenuName());
                if(list.size()>0){
                    map.put("flag",flag);
                    rs.setData(map);
                    rs.setErrorMsg("菜单名称已经存在！");
                    return rs;
                }
            }else {  // 修改
                List<TSysMenu> list = MenuService.findMenuListByMenuName(sysMenu.getId(), sysMenu.getMenuName());
                if (list.size() == 0) { //菜单名称已改
                    List<TSysMenu> list1 = MenuService.findMenuListByMenuName(null, sysMenu.getMenuName());
                    if (list1.size() > 0) {
                        map.put("flag",flag);
                        rs.setData(map);
                        rs.setErrorMsg("菜单名称已经存在！");
                        return rs;
                    }
                }
            }
            flag = true;
            map.put("flag",flag);
            rs.setData(map);
            LOG.info("验证菜单名称是否唯一成功！");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("验证菜单名称是否唯一失败！");
            e.printStackTrace();
        }
        return rs;
    }

    @ApiOperation(value = "删除菜单",notes = "删除菜单",httpMethod = "GET")//描述接口
    @RequestMapping("/deleteMenu")
    public ResultData deleteMenu(String menuId, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
            rs.setSuccess(false);
            if(StringUtils.isBlank(menuId)){
                rs.setErrorMsg("菜单id不能为空！");
                return rs;
            }
            if(MenuService.hasChildByMenuId(menuId)){
                rs.setErrorMsg("存在子菜单,不允许删除");
                return rs;
            }
            if(MenuService.checkMenuExistRole(menuId)){
                rs.setErrorMsg("菜单已分配,不允许删除");
                return rs;
            }
            MenuService.deleteMenuById(menuId);
            rs.setSuccess(true);
            LOG.info("删除菜单成功！");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("删除菜单失败！");
            e.printStackTrace();
        }
        return rs;
    }
}
